package com.sanyang.pda.ui.manualoutbound;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sanyang.pda.adapter.ManualOutBoundAdapter;
import com.sanyang.pda.bean.ManualOutBoundBean;
import com.sanyang.pda.bean.ManualPickingBean;
import com.sanyang.pda.contract.ManualOutBoundContract;
import com.sanyang.pda.presenter.ManualOutBoundPresenter;
import com.sanyang.pda.utils.StringConfig;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpActivity;
import com.wangshen.projectbase.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = AppActivityKey.MANUALOUTBOUNDACTIVITY)
public class ManualOutBoundActivity extends BaseMvpActivity<ManualOutBoundPresenter> implements ManualOutBoundContract.View, OnRefreshListener, OnLoadMoreListener {


    @BindView(R.id.title)
    FrameLayout title;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.smart)
    SmartRefreshLayout smart;
    private ManualOutBoundAdapter manualWarehousingAdapter;
    private List<ManualOutBoundBean> data = new ArrayList<>();
    private int num=1;
    @Override
    public int getView() {
        return R.layout.activity_manual_outbound;
    }

    @Override
    public void initView() {
        smart.setOnRefreshListener(this);
        smart.setOnLoadMoreListener(this);
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        manualWarehousingAdapter = new ManualOutBoundAdapter(R.layout.item_manual_outbound, new ArrayList<>());
        rvList.setAdapter(manualWarehousingAdapter);
    }

    @Override
    public void initLinsenterner() {
        manualWarehousingAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                    List<ManualPickingBean> list = (List<ManualPickingBean>) adapter.getData();
                    Bundle bundle = new Bundle();
                    bundle.putString(StringConfig.DELIVERYNO, list.get(position).getDeliveryNo());
                    startIntent(AppActivityKey.MANUALOUTBOUNDDETAILACTIVITY, bundle);
                }
            });
    }

    @Override
    public void getData() {
        smart.autoRefresh();
    }

    @Override
    public ManualOutBoundPresenter getPresenter() {
        return new ManualOutBoundPresenter();
    }

    @Override
    public void onLoadMore(RefreshLayout refreshLayout) {
        num++;
        presenter.manualPickingList(num);
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        num=1;
        presenter.manualPickingList(num);
    }


    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finishActivity();
    }

    @Override
    public void setList(List<ManualPickingBean> list) {
        smart.finishLoadMore();
        smart.finishRefresh();
        if (num == 1){
            manualWarehousingAdapter.setNewInstance(list);
        }else {
            manualWarehousingAdapter.addData(list);
        }
    }

}
